/**
 * Created by Administrator on 2018/7/27.
 */
var $CertusFormAjax;
var $CertusJsonAjax;
var $$scope;
function initDocument($scope, $route, CertusFormAjax, CertusJsonAjax, $location, $http, $routeParams) {
    $CertusFormAjax = CertusFormAjax;
    $CertusJsonAjax = CertusJsonAjax;
    $$scope = $scope;

    var fileCate = $routeParams.fileCate;
    //初始化 文件抬头
    if (fileCate == "YF") {
        $scope.datagridname = "研发类项目";
    } else if (fileCate == "SC") {
        $scope.datagridname = "市场类项目";
    } else if(fileCate == "XZ") {
        $scope.datagridname = "行政";
    } else {
        $scope.datagridname = '其他类文档';
    }
    //初始化 datagrid
    CertusJsonAjax.dataInit("documentDatagrid", {fileCategories: fileCate}, yfColumns, "yfContainer1", "yfToolBarContainer1");
}
var yfColumns = [{
    id: 'fileClass',
    title: '文件类型',
    type: 'string',
    width: 200,
    columnClass: 'text-center',
}, {
    id: 'proName',
    title: '项目名称',
    type: 'string',
    width: 110,
    columnClass: 'text-center',
},
    {
        id: 'fileOriginName',
        title: '文件名称',
        type: 'string',
        // width: 200,
        columnClass: 'text-center',
    }, {
        id: 'fileDescriptor',
        title: '文件说明',
        type: 'string',
        columnClass: 'text-left',
        hideType: 'lg|md|sm|xs'
    }, {
        id: 'version',
        title: '文件版本',
        type: 'string',
        width: 73,
        columnClass: 'text-center',
    }, {
        id: 'userName',
        title: '上传人员',
        type: 'string',
        width: 73,
        columnClass: 'text-center',
    }, {
        id: 'createTime',
        title: '更新时间',
        type: 'string',
        width: 100,
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var date = new Date(record.createTime);
            var content = $.fn.dlshouwen.grid.tools.dateFormat(date, "yyyy-MM-dd hh:mm");
            return content;
        }
    }, {
        id: 'operation',
        title: '操作',
        width: 80,
        type: 'string',
        columnClass: 'text-center',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var content = '';
            content += '<button class="btn btn-xs btn-info" onclick="down(\'' + record.id + '\')"><i class="icon-cloud-download"></i>  下载</button>';
            return content;
        }
    }];

//点击 下载 触发 方法
function down(pathId) {
    $CertusFormAjax.ajax("preDownload", {pathId: pathId}, function (res) {
        if (res.data == "downNow") {
            $CertusFormAjax.download("downloadFile", "fileId=" + pathId);
        }else if(res.data == "overTime") {
            layer.msg("此文件已经过期");
        } else if (res.data == "appling") {
            layer.msg("流程正在审批");
        }else if (res.data == "denied") {
            layer.msg("审批不通过，无法进行下载");
        } else if (res.data == "needApply") {
            layer.confirm('对此文件暂时没有下载权限，是否申请', {
                title: '提示',
                btn: ['申请', '取消'] //按钮
            }, function () {
                //开始申请 权限
                $$scope.applyFile = {};
                //templateCode
                $$scope.applyFile.templateCode = "WJXZQXSPLC";
                $$scope.applyFile.pathId = pathId;
                $CertusJsonAjax.ajax("startApplyDownload", $$scope.applyFile, function (res) {
                    if (res.data == "success") {
                        layer.msg("权限申请成功，请在 由我发起 中查看进度");
                    }
                })


            });
        }
    })
}

